package org.mathig.poker.action;

import org.mathig.poker.PlayerWrapper;
import org.mathig.poker.table.TableHand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BetHelper {

	private static Logger logger = LoggerFactory.getLogger(BetHelper.class);

	private BetHelper() {
	}

	public static int checkBet(TableHand tableHand, PlayerWrapper player, int addToBet) {
		// how player add to its previous bet ?
		int oldStack = tableHand.getStack(player);
		int oldBet = tableHand.getBet(player);

		// bet must be >= BB (if stack is enough)
		int bb = tableHand.getBigBlind();
		if (addToBet < bb && bb <= oldStack) {
			logger.warn("{} bet {} less than Big Blind {} ", new Object[] { player, addToBet, bb });
			addToBet = bb;
		}

		// bet must be >= highest bet (if stack is enough)
		int maxBet = tableHand.getBet();
		int diffToCall = maxBet - oldBet;
		if (addToBet < diffToCall && diffToCall <= oldStack) {
			logger.warn("{} add {} to previous bet {} but it is not enough to call highest bet {} ", new Object[] { player, addToBet, oldBet, maxBet });
			addToBet = maxBet - oldBet;
		}

		// bet must be <= all-in
		if (addToBet > oldStack) {
			logger.warn("{} add {} what is more than its stack {} ", new Object[] { player, addToBet, oldStack });
			addToBet = oldStack;
		}

		return addToBet;
	}

	public static void addToBet(TableHand tableHand, PlayerWrapper player, int addToBet) {
		tableHand.getTable().getStacks().remove(player, addToBet);
		logger.debug("stack {} = {}", player, tableHand.getStack(player));
		tableHand.getBets().add(player, addToBet);
		logger.debug("{}'s bet is now {}", player, tableHand.getBet(player));
	}
}
